import storage from 'store'
import { getInfo } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'


const user = {
  state: {
    info: {}//综合信息
  },

  mutations: {
    SET_INFO: (state, info) => {
      state.info = info
    },
  },

  actions: {
  

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const result = response.result
          if (result && result.roles && result.menus.length > 0) {
            const info = (({accountNo,empName,empNo,profilePicture,roles,buttons,sysEmp}) => ({accountNo,empName,empNo,profilePicture,roles,buttons,sysEmp}))(result)
            commit('SET_INFO', info)
          } else {
            reject(new Error('此账号没有本模块的权限 !'))
          }

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
    
      /*
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_INFO', {})
          storage.remove(ACCESS_TOKEN)
          resolve()
        })
      })
      */
     
        commit('SET_INFO', {})
      //storage.remove(ACCESS_TOKEN)
        sessionStorage.removeItem(ACCESS_TOKEN);
    }

  }
}

export default user
